//
//  YKClientDrugPriceDetailDetailItemCell.m
//  YunKang
//
//  Created by leoking870 on 2017/10/17.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKClientDrugPriceDetailDetailItemCell.h"
#import "YKDashView.h"
#import <UIImageView+WebCache.h>
#import "KYVideoCheckBox.h"
#import "YKSelectDrugModel.h"
#import "YKSelectDrugItem.h"
@interface YKClientDrugPriceDetailDetailItemCell ()
@property(weak, nonatomic) IBOutlet UIImageView *drugImagView;
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;

@property (weak, nonatomic) IBOutlet UILabel *packLabel;
@property (weak, nonatomic) IBOutlet UILabel *codeLabel;
@property (weak, nonatomic) IBOutlet UILabel *companyLbl;

@property(weak, nonatomic) IBOutlet UILabel *submitPersonLabel;
@property(weak, nonatomic) IBOutlet UIView *view;

@property(weak, nonatomic) IBOutlet UIView *priceInfoView;
@property (weak, nonatomic) IBOutlet UILabel *buyPriceLabel;

@property(weak, nonatomic) IBOutlet UILabel *wholePriceLabel;
@property(weak, nonatomic) IBOutlet UITextField *drugPriceTF;
@property(weak, nonatomic) IBOutlet UILabel *lowestPriceLabel;
@property (weak, nonatomic) IBOutlet UITextField *scoreTF;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageWidth;
@property (nonatomic, strong) KYVideoCheckBox* box;
@end

@implementation YKClientDrugPriceDetailDetailItemCell
@dynamic tableViewItem;

- (void)cellLoadSubViews {
    [super cellLoadSubViews];
    UIView *view = [[UIView alloc]init];
    [self.contentView addSubview:view];
    
    KYVideoCheckBox* box = [[KYVideoCheckBox alloc]initWithFrame:CGRectMake(0, 0, 15, 15)];
    box.on = NO;
    [view addSubview:box];
    view.translatesAutoresizingMaskIntoConstraints = NO;
    [box mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(view);
        make.size.mas_equalTo(CGSizeMake(15, 15));
        make.height.equalTo(view.mas_height);
    }];
    self.box = box;
    NSArray *views = @[view, self.view, self.priceInfoView];
    if ([self.tableViewItem.model isKindOfClass:[YKSelectDrugModel class]]) {
        view.tf_weight = 1;
        [box addTarget:self action:@selector(onCheckBoxValueChanged:) forControlEvents:UIControlEventValueChanged];
    }
    else{
        views = @[self.view, self.priceInfoView];
        view.hidden = YES;
    }
    self.view.tf_weight = 3;
    self.priceInfoView.tf_weight = 2;

    
//    self.operatorLabel.textAlignment = NSTextAlignmentCenter;

    [views mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.mas_centerY);
//        make.top.equalTo(self.contentView.mas_top).offset(11);
    }];
    [self.priceInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(8);
    }];

    [views mas_alignWeightlyAlongAxis:MASAxisTypeHorizontal
                                                      fixedSpace:@0
                                                       leadingTo:self.contentView.mas_leading
                                                       leadSpace:3
                                                      trailingTo:self.contentView.mas_trailing
                                                       tailSpace:3];
    
    
//        [RACObserve(self.drugPriceTF, text) subscribeNext:^(id x) {
//            self.tableViewItem.model.DRUGPRICE = x;
//        }];
//
        [[self.drugPriceTF.rac_textSignal skip:1] subscribeNext:^(id x) {
            self.tableViewItem.model.DRUGPRICE = x;
            self.drugPriceTF.textColor = [x floatValue] >= self.tableViewItem.model.LOWEST_PRICE.floatValue ? [UIColor tf_colorWithHexString:@"#717071"] : [UIColor redColor];
        }];
    
//    [RACObserve(self.scoreTF, text) subscribeNext:^(id x) {
//        self.tableViewItem.model.GIVE_INTEGRAL = x;
//    }];
    
    [[self.scoreTF.rac_textSignal skip:1] subscribeNext:^(id x) {
        self.tableViewItem.model.GIVE_INTEGRAL = x;
    }];
}


- (void)cellWillAppear {
    [super cellWillAppear];
    //http://yunkangmall.njnwyy.com:8084/ccenrun-admins-mo/
    [self.drugImagView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://yunkangmall.njnwyy.com:8084%@", self.tableViewItem.model.FACE_PIC1]] placeholderImage:[UIImage imageNamed:@"yk_order_item_img"]];
    
    self.drugPriceTF.text = self.tableViewItem.model.DRUGPRICE;
    self.scoreTF.text = self.tableViewItem.model.GIVE_INTEGRAL;
    self.wholePriceLabel.text = self.tableViewItem.model.WHOLE_PRICE;
    self.lowestPriceLabel.text = self.tableViewItem.model.LOWEST_PRICE;
    self.scoreTF.enabled = NO;
    self.drugPriceTF.enabled = NO;
    self.submitPersonLabel.text = self.tableViewItem.model.DRUGNAME;
    self.packLabel.text = self.tableViewItem.model.CPACKINGTYPE;//[NSString stringWithFormat:@"%@", , ];
    self.companyLbl.text = self.tableViewItem.model.CENTERPRISE;
    
    if ([self.tableViewItem.model isKindOfClass:[YKSelectDrugModel class]]) {
        
        self.scoreTF.enabled = YES;
        self.drugPriceTF.enabled = YES;
        self.scoreTF.clearButtonMode = UITextFieldViewModeNever;
        self.drugPriceTF.clearButtonMode = UITextFieldViewModeNever;
        self.scoreTF.keyboardType = UIKeyboardTypeDecimalPad;
        self.drugPriceTF.keyboardType = UIKeyboardTypeDecimalPad;
        self.scoreTF.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.drugPriceTF.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.scoreTF.layer.borderWidth = .5f;
        self.drugPriceTF.layer.borderWidth = .5f;
        self.scoreTF.layer.cornerRadius = 2.f;
        self.drugPriceTF.layer.cornerRadius = 2.f;
        self.drugPriceTF.layer.masksToBounds = YES;
        self.scoreTF.layer.masksToBounds = YES;
        YKSelectDrugItem *item = self.tableViewItem;
        self.box.on = item.selected;
        self.imageWidth.constant = 0;
        self.codeLabel.text = [item.model CINVMNEM_CODE];
        
    }
    else {
        self.codeLabel.text = self.tableViewItem.model.DRUGCODE;
        self.imageWidth.constant = 55;
        self.scoreTF.enabled = NO;
        self.drugPriceTF.enabled = NO;
//        self.scoreTF.backgroundColor = [UIColor tf_colorWithHexString:@"#717071"];
//        self.drugPriceTF.backgroundColor = [UIColor tf_colorWithHexString:@"#717071"];
    }
    self.scoreLabel.text = [NSString stringWithFormat:@"参考积分:%@",self.tableViewItem.model.PERFORMANCE_POINTS];
    self.buyPriceLabel.text = self.tableViewItem.model.PURCHASE_PRICE?:@"0";
    self.drugPriceTF.textColor = [self.tableViewItem.model.DRUGPRICE floatValue] >= self.tableViewItem.model.LOWEST_PRICE.floatValue ? [UIColor tf_colorWithHexString:@"#717071"] : [UIColor redColor];
}

- (void)onCheckBoxValueChanged:(KYVideoCheckBox *)sender {
    YKSelectDrugItem *item = self.tableViewItem;
    item.selected = sender.on;
}


@end
